# JUDICIAL PROFESSIONAL BACKGROUND AND PRETRIAL DETENTION OUTCOMES
# Oded Oren and Chad Topaz
# Outputs: Table 11 (NVFO/Misd subset)
library("this.path")
source(paste0(dirname(this.path()), "/functions.R"))

# Data Loading --------------------------------------------------
judgeinfo <- readRDS(paste0(dirname(this.path()), "/data/judgeinfo.RDS")) %>%
  select(-detailed_experience)

detentiondata <- readRDS(paste0(dirname(this.path()), "/data/detentiondata.RDS")) %>%
  filter(topseverity == "NVFO" | topseverity == "Misdemeanor") %>%
  droplevels()

baildata <- readRDS(paste0(dirname(this.path()), "/data/baildata.RDS")) %>%
  filter(topseverity == "NVFO" | topseverity == "Misdemeanor") %>%
  droplevels()

# Experience vs. None (Main specification for Table 11)-----
## Detention Model ------------------------------------------------------------
vars_detention <- setdiff(names(detentiondata), c("detained", "judge", "law_enforcement", "legal_services"))

model_detention <- feols(
  as.formula(paste("detained ~", paste(vars_detention, collapse = " + "))),
  data = detentiondata,
  cluster = ~ judge)
summary(model_detention)

## Bail Extensive Model -------------------------------------------------------
data_extensive <- baildata %>%
  mutate(bail_set = cash.bail > 0)

vars_extensive <- setdiff(
  names(data_extensive),
  c("bail_set",
    "cash.bail",
    "judge",
    "law_enforcement",
    "legal_services")
)

model_extensive <- feols(
  as.formula(paste("bail_set ~", paste(vars_extensive, collapse = " + "))),
  data    = data_extensive,
  cluster = ~ judge
)

summary(model_extensive)

## Bail Intensive Model -------------------------------------------------------
data_intensive = baildata %>%
  filter(cash.bail > 0) %>%
  mutate(log_bail = log(cash.bail))

model_intensive <- lm(
  log_bail ~ . - judge - cash.bail - law_enforcement - legal_services - log_bail,
  data = data_intensive)

vcov_intensive  <- vcovCL(model_intensive,
                          data = data_intensive,
                          cluster = ~judge,
                          type = "HC1")

print(coeftest(model_intensive, vcov = vcov_intensive))

# Law Enforcement vs. All Others (Alternative specification)------
## Detention Model ------------------------------------------------------------
vars_detention_LE <- setdiff(
  names(detentiondata),
  c("detained", "judge", "experience",
    "legal_services")
)

model_detention_LE <- feols(
  as.formula(paste("detained ~", paste(vars_detention_LE, collapse = " + "))),
  data    = detentiondata,
  cluster = ~ judge
)
summary(model_detention_LE)

## Bail Extensive Model -------------------------------------------------------
data_extensive_LE <- baildata %>%
  mutate(bail_set = cash.bail > 0)

vars_extensive_LE <- setdiff(
  names(data_extensive_LE),
  c("bail_set",
    "cash.bail",
    "judge",
    "experience",
    "legal_services")
)

model_extensive_LE <- feols(
  as.formula(paste("bail_set ~", paste(vars_extensive_LE, collapse = " + "))),
  data    = data_extensive_LE,
  cluster = ~ judge
)

summary(model_extensive_LE)

## Bail Intensive Model -------------------------------------------------------
data_intensive_LE <- baildata %>%
  filter(cash.bail > 0) %>%
  mutate(log_bail = log(cash.bail))

model_intensive_LE <- lm(
  log_bail ~ . -judge -cash.bail -experience -legal_services -log_bail,
  data = data_intensive_LE
)

vcov_intensive_LE <- vcovCL(model_intensive_LE,
                            data = data_intensive_LE,
                            cluster = ~judge,
                            type    = "HC1")

print(coeftest(model_intensive_LE, vcov = vcov_intensive_LE))

# Legal Services vs. All Others (Alternative specification) -----
## Detention Model ------------------------------------------------------------
vars_detention_LS <- setdiff(
  names(detentiondata),
  c("detained", "judge", "experience", "law_enforcement")   # drop the other two background dummies
)

model_detention_LS <- feols(
  as.formula(paste("detained ~", paste(vars_detention_LS, collapse = " + "))),
  data    = detentiondata,
  cluster = ~ judge
)
cat("\n=== Detention model (legal_services) ===\n")
summary(model_detention_LS)

## Bail Extensive Model -------------------------------------------------------
data_extensive_LS <- baildata %>%
  mutate(bail_set = cash.bail > 0)

vars_extensive_LS <- setdiff(
  names(data_extensive_LS),
  c("bail_set",
    "cash.bail",
    "judge",
    "experience",
    "law_enforcement")
)

model_extensive_LS <- feols(
  as.formula(paste("bail_set ~", paste(vars_extensive_LS, collapse = " + "))),
  data    = data_extensive_LS,
  cluster = ~ judge
)

summary(model_extensive_LS)

## Bail Intensive Model -------------------------------------------------------
data_intensive_LS <- baildata %>%
  filter(cash.bail > 0) %>%
  mutate(log_bail = log(cash.bail))

model_intensive_LS <- lm(
  log_bail ~ . -judge -cash.bail -experience -law_enforcement -log_bail,
  data = data_intensive_LS
)

vcov_intensive_LS <- vcovCL(model_intensive_LS,
                            data = data_intensive_LS,
                            cluster = ~judge,
                            type    = "HC1")

print(coeftest(model_intensive_LS, vcov = vcov_intensive_LS))

# Generate Table 11-----
table_11(
    model_detention,  model_extensive,  model_intensive,  vcov_intensive,
    model_detention_LE, model_extensive_LE, model_intensive_LE, vcov_intensive_LE,
    model_detention_LS, model_extensive_LS, model_intensive_LS, vcov_intensive_LS
)
